package com.kingstar.ceres.data.properties;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.kingstar.ceres.data.bo.BaseFileMapping;
import com.kingstar.ceres.data.bo.CsvDataConfig;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@Data
public class FileMappingProperties {

    @JacksonXmlElementWrapper(localName = "ExcelFileMappings")
    private List<ExcelFileMapping> excelFileMapping;

    @JacksonXmlElementWrapper(localName = "CsvFileMappings")
    private List<CsvFileMapping> csvFileMapping;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ExcelFileMapping extends BaseFileMapping {
        private String entityClazz;
        private String serviceClazz;
        public ExcelFileMapping init(String fileId, String fileName, String localPath, String remotePath, String fileType){
            super.init(fileId,fileName,localPath,remotePath,fileType);
            return this;
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class CsvFileMapping extends BaseFileMapping{
        private String headers;
        private String tableCols; //对应插入的数据表的列名
        private String ignoreIdx;//忽略的文本文件中数据列索引，索引从0 开始
        private String colTypes;//对应列的类型,参考FileImportConstants常量  由于表达式可能存在`,`所以用`;`作为分割符
        private String hasHead;
        private String regex;
        //字段是否会被引号包裹 0-不需引号包裹,1-需被引号包裹
        private int quoteCharWrap;
        //是否需要生成主键id 0-不需要,1-需要
        private int generateId;
        public CsvFileMapping init(String fileId, String fileName, String localPath, String remotePath, String fileType){
            super.init(fileId,fileName,localPath,remotePath,fileType);
            return this;
        }
        public char formatRegex(){
            if (StringUtils.isNumeric(regex)){
                return (char)Integer.parseInt(regex);
            }
            return regex.charAt(0);
        }

        public CsvDataConfig toCsvDataConfig(String opDate){
            return new CsvDataConfig(getFileId(), this.headers,this.ignoreIdx,this.tableCols, this.colTypes,
                    formatRegex(),getFileType(),opDate,this.generateId,getFileDate(),getInsertHolder());
        }
    }
}
